package com.td.erhome.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.models.MoreBean;
import com.td.erhome.utils.RxBus;

/**
 * Created by admin on 2017/2/14.
 */

public class PopMoreAdapter extends RecyclerView.Adapter<PopMoreAdapter.MyViewHolder> {
    private Context mContext;
    private int layoutPosition;
    private String[] mList;
    private int type;

    public PopMoreAdapter(Context context, String[] data,int layoutPosition,int type){
        this.mContext = context;
        this.mList = data;
        this.layoutPosition = layoutPosition;
        this.type = type;

    }

    public void resetPoint(int layoutPosition){
        this.layoutPosition = layoutPosition;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = View.inflate(mContext, R.layout.pop_more_item, null);
        MyViewHolder holder = new MyViewHolder(itemView);
        return holder;
    }

    @Override

    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        holder.mTvItem.setText(mList[position]);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前点击的位置
                layoutPosition = holder.getLayoutPosition();
                notifyDataSetChanged();
                RxBus.getInstance().post(new MoreBean(layoutPosition,type));
            }
        });

        //更改状态
        if(position == layoutPosition){
            holder.mTvItem.setBackgroundResource(R.drawable.shape_status); //选中
            holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_048d99));

        }else{
            holder.mTvItem.setBackgroundResource(R.drawable.shape_status_two);
            holder.mTvItem.setTextColor(mContext.getResources().getColor(R.color.color_666666));
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.length;
    }
    class MyViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTvItem;

        public MyViewHolder(View itemView) {
            super(itemView);
            mTvItem = (TextView) itemView.findViewById(R.id.tv_item);
        }
    }
}
